<?php
require_once dirname(__FILE__) . '/InterfaceBase.php';
class BillingInterface extends InterfaceBase implements IService
{
    const BIILING_NOTES_DEFAULT = 'OpenIII';
    const BILLING_TIME_TYPE_DEFAULT = 5;
    # 区添加操作
    const BILLING_AREA_OP_ADD = 'add';
    # 区更新操作
    const BILLING_AREA_OP_UPDATE = 'modify';
    # 区删除操作
    const BILLING_AREA_OP_DELETE = 'delete';
    public function __construct ($env = self::APP_ENV_TEST)
    {
        parent::__construct($env);
        $this->loadSdk("SDOServiceBilling");
    }
    /**
     * 开通计费服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * spId=>商户ID
     * app=>array( appName=>应用名称, timeTypeId=>计费类型,)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function open ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $spId = $args['spId'];
        $appInfo = $args['app'];
        $this->processLog(__CLASS__, 'addApp');
        if (! SDOServiceBilling::addApp($msg, $appId, $appInfo['appName'], $sdkEnv, $appInfo['timeTypeId'], $spId, 
        self::BIILING_NOTES_DEFAULT)) {
            $this->setError(SDOServiceBilling::getErrNo(), $msg);
            $this->endLog(__CLASS__);
            return false;
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 关闭计费服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * spId=>商户ID,
     * areas=>array(1,2,3...)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function close ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $spId = $args['spId'];
        if (isset($args['areas']) && $args['areas']) {
        	$this->processLog(__CLASS__, 'deleteArea');
            foreach ($args['areas'] as $areaId) {
                if (! SDOServiceBilling::deleteArea($msg, $appId, $sdkEnv, $areaId, 
                'default', self::BIILING_NOTES_DEFAULT)) {
                    $this->setError(SDOServiceBilling::getErrNo(), $msg);
                    $this->endLog(__CLASS__);
                    return false;
                }
            }
        }
        $this->processLog(__CLASS__, 'deleteApp');
        if (! SDOServiceBilling::deleteApp($msg, $appId, 'default', $sdkEnv, self::BILLING_TIME_TYPE_DEFAULT, $spId, 
        self::BIILING_NOTES_DEFAULT)) {
            $this->setError(SDOServiceBilling::getErrNo(), $msg);
            $this->endLog(__CLASS__);
            return false;
        }
        $this->endLog(__CLASS__);
        return true;
    }
    /**
     * 修改计费服务
     * @param array $message array("errNo"=>错误码, "errMsg"=>错误信息)
     * @param array $args array(
     * appId=>应用ID,
     * spId=>商户ID
     * app=>array( appName=>应用名称, timeTypeId=>计费类型,)
     * )
     * @param int $env 环境变量
     * @return boolean
     */
    public function update ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $spId = $args['spId'];
        $appInfo = $args['app'];
        $this->processLog(__CLASS__, 'modifyApp');
        if (! SDOServiceBilling::modifyApp($msg, $appId, $appInfo['appName'], $sdkEnv, $appInfo['timeTypeId'], $spId, self::BIILING_NOTES_DEFAULT)) {
            $this->setError(SDOServiceBilling::getErrNo(), $msg);
            $this->endLog(__CLASS__);
            return false;
        }
        $this->endLog(__CLASS__);
        return true;
    }
    public function get ($args, $env = null)
    {}
    /**
     * 同步
     * @param array $message
     * @param array $args array(
     * 'appId'=>应用ID,
     * 'areas'=>array(
     * array(areaId=>1, areaName=>'区名称', op=>'操作'),
     * array(areaId=>1, areaName=>'区名称', op=>BillingInterface::BILLING_AREA_OP_UPDATE)
     * ),
     * )
     * @param mixed $env
     */
    public function sync ($args, $env = null)
    {
        if (! $env) {
            $env = $this->env;
        }
        $this->startLog($args, $env, __FUNCTION__, __CLASS__);
        $msg = '';
        $sdkEnv = self::$ENVType[$env];
        $appId = $args['appId'];
        $areas = $args['areas'];
        foreach ($areas as $row) {
            $method = $row['op'] . "Area";
            $this->processLog(__CLASS__, $method);
            if (! SDOServiceBilling::$method($msg, $appId, $sdkEnv, $row['areaId'], $row['areaName'], 
            self::BIILING_NOTES_DEFAULT)) {
                $this->setError(SDOServiceBilling::getErrNo(), $msg);
                $this->endLog(__CLASS__);
                return false;
            }
        }
        $this->endLog(__CLASS__);
        return true;
    }
}